#include "cinputhandler.h"
#include "../camera/camera.h"

namespace NEngine {

// === CCameraInputHandler ========================================================================

// ================================================================================================
// Toggle mouse
// ================================================================================================
void CCameraInputHandler::ToggleMouse(bool toggle) {
    if(toggle) {
        ShowCursor(false);
		ClipCursor(&rect);
        center.x = (rect.left + rect.right) / 2;
        center.y = (rect.top + rect.bottom) / 2;
        SetCursorPos(center.x, center.y);
    }
    else {
        ShowCursor(true);
        ClipCursor(NULL);
    }
}

// ================================================================================================
// Listen to mouse input
// ================================================================================================
void CCameraInputHandler::ListenMouse() {
    if(!usemouse)
        return;

    // -- get the current position
    GetCursorPos(&endpt);

    const int deltaptx = endpt.x - center.x;
    const int deltapty = endpt.y - center.y;

    if(deltaptx != 0)
        (*curcam)->RotateViewY(float(deltaptx) * mousescale);
    if(deltapty != 0)
        (*curcam)->RotateViewX(float(-deltapty) * mousescale);

    // -- refresh the cursors
    SetCursorPos(center.x, center.y);
}

// ================================================================================================
// Listen to input
// ================================================================================================
bool CCameraInputHandler::Listen() {
    if(GetAsyncKeyState(eCKey) != 0) {
        stpwtch.Click();
        if(stpwtch.GetDeltaSeconds() >= deltime) {
            ++camindex;
            camindex = camindex%numcameras;
            *curcam = base + camindex;
        }
    }

    if(GetAsyncKeyState(VK_LEFT) != 0) {
        (*curcam)->RotateViewY(-0.002f);
    }

    if(GetAsyncKeyState(VK_RIGHT) != 0) {
        (*curcam)->RotateViewY(0.002f);
    }

    if(GetAsyncKeyState(VK_UP) != 0) {
        Vector4 up(0.0f, 0.002f, 0.0f);
        (*curcam)->Translate(up);
    }

    if(GetAsyncKeyState(VK_SPACE) != 0) {
        Vector4 up(0.0f, 0.002f, 0.0f);
        (*curcam)->Translate(up);
    }

    if(GetAsyncKeyState(VK_DOWN) != 0) {
        Vector4 up(0.0f, -0.002f, 0.0f);
        (*curcam)->Translate(up);
    }

    if(GetAsyncKeyState(VK_CONTROL) != 0) {
        Vector4 up(0.0f, -0.002f, 0.0f);
        (*curcam)->Translate(up);
    }

    if(GetAsyncKeyState(eAKey) != 0) {
        Vector4 left(-0.002f, 0.0f, 0.0f);
        (*curcam)->Translate(left);
    }

    if(GetAsyncKeyState(eDKey) != 0) {
        Vector4 left(0.002f, 0.0f, 0.0f);
        (*curcam)->Translate(left);
    }

    if(GetAsyncKeyState(eSKey) != 0) {
        Vector4 at(0.0f, 0.0f, 0.002f);
        (*curcam)->Translate(at);
    }

    if(GetAsyncKeyState(eWKey) != 0) {
        Vector4 at(0.0f, 0.0f, -0.002f);
        (*curcam)->Translate(at);
    }

    if(GetAsyncKeyState(eTKey) != 0) {
        stpwtch.Click();
        if(stpwtch.GetDeltaSeconds() >= deltime) {
            usemouse = !usemouse;
            ToggleMouse(usemouse);
        }
    }

    if(GetAsyncKeyState(VK_ESCAPE) != 0) {
        kill = true;
    }

    ListenMouse();

    return kill;
}


} // namespace NEngine